package com.winning.devops.batch.reader.ch07.config;

import com.winning.devops.batch.reader.ch07.domain.Customer;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;

/**
 * 从多个文件中读取数据
 * <p>
 * 多个文件为 resources目录下 t1~t3.txt三个文件
 *
 * @author chensj
 * @date 2020-03-04 21:00
 */
@Configuration
public class ErrorFilesJobDemoConfig {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    @Value("classpath:t*.txt")
    private Resource[] fileResources;
    @Autowired
    @Qualifier("errorFlatFileItemWriter")
    private ItemWriter<? super Customer> errorFlatFileItemWriter;
    @Autowired
    @Qualifier("restartReader")
    private ItemReader<Customer> restartReader;


    @Bean
    public Job errorFilesJobDemoJob() {
        return jobBuilderFactory.get("errorFilesJobDemoJob")
                .start(errorFilesJobDemoStep())
                .build();
    }

    @Bean
    public Step errorFilesJobDemoStep() {
        return stepBuilderFactory
                .get("errorFilesJobDemoStep")
                .<Customer, Customer>chunk(10)
                .reader(restartReader)
                .writer(errorFlatFileItemWriter)
                .build();

    }
}
